package com.groupbuying.cheetah.temp;

import com.groupbuying.cheetah.common.PageUtils;
import com.groupbuying.cheetah.common.Query;
import com.groupbuying.cheetah.order.storeFlow.StoreOrderFlowDAO;
import com.groupbuying.cheetah.utils.DesensitizationUtils;
import com.groupbuying.cheetah.utils.JsonUtils;
import com.groupbuying.cheetah.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/financeOrder")
public class FinanceOrderController {

   private Logger logger = LoggerFactory.getLogger(FinanceOrderController.class);

    @Autowired
    private StoreOrderFlowDAO storeOrderFlowDAO;

    @Autowired
    private FinanceOrderDAO financeOrderDAO;

    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
//        Query query = new Query(params);
//        List<Map<String, String>> storeMoneyFlowList = financeOrderDAO.list(query);
//        int total = financeOrderDAO.count(params);
//        PageUtils pageUtils = new PageUtils(storeMoneyFlowList, total);
//        return pageUtils;

        String payType = (String) params.get("pay_type");
        List<String> list = new ArrayList();
        if("现金支付".equals(payType)){
            list.add("微信支付");
            list.add("支付宝支付");
        }else if(StringUtils.isEmpty(payType)){
            list.add("微信支付");
            list.add("支付宝支付");
            list.add("积分支付");
            list.add("余额支付");
        }else{
            list.add(payType);
        }
        params.remove("pay_type");
        params.put("payTypes",list);
        Query query = new Query(params);
        logger.error("StoreOrderFlowController.list()-->接受的参数信息:"+ params.toString());
        List<Map<String, String>> storeOrderFlowList = storeOrderFlowDAO.storeOrderFlow(query);
        logger.error("StoreOrderFlowController.list()-->storeOrderFlowList:"+ JsonUtils.objectToJson(storeOrderFlowList));
        int total = storeOrderFlowDAO.count(params);
//        if(storeOrderFlowList.size() > 0 && storeOrderFlowList != null){
//            for(Map<String, String> map:storeOrderFlowList){
//                String customerMobile = map.get("customer_mobile");
//                map.put("customer_mobile", DesensitizationUtils.desensitizePhoneNumber(customerMobile));
//            }
//        }
        PageUtils pageUtils = new PageUtils(storeOrderFlowList, total);
        logger.error("StoreOrderFlowController.list()-->从pageUtils返回信息为:"+ pageUtils.getRows());
        logger.error("StoreOrderFlowController.list()-->从pageUtils返回信息为:"+ pageUtils.getTotal());
        return pageUtils;
    }

    @GetMapping("/aggregation")
    public ApiResult aggregation(@RequestParam Map<String, Object> params) {
        try{
            String payType = (String) params.get("pay_type");
            List<String> list = new ArrayList();
            if("现金支付".equals(payType)){
                list.add("微信支付");
                list.add("支付宝支付");
            }else if(StringUtils.isEmpty(payType)){
                list.add("微信支付");
                list.add("支付宝支付");
                list.add("积分支付");
                list.add("余额支付");
            }else{
                list.add(payType);
            }
            params.remove("pay_type");
            params.put("payTypes",list);
            logger.info("StoreOrderFlowController.amountStatistics()"+params);
            Map<String, String> result = storeOrderFlowDAO.amountStatistics(params);
            logger.info("StoreOrderFlowController.amountStatistics()"+result);
            if(result == null || result.size()<=0){
                result = new HashMap();
                result.put("payMoney","0");
                result.put("slottingAllowance","0");
                result.put("platformProfit","0");
            }
            return ApiResult.success(result);
        }catch(Exception e){
            logger.error("StoreOrderFlowController.amountStatistics()",e);
            return ApiResult.failure();
        }
    }


//  @GetMapping("/aggregation")
//  public ApiResult aggregation(@RequestParam Map<String, Object> params) {
//      try{
//          logger.info("FinanceOrderController-->aggregation params:::"+params);
//          Map<String, String> result = financeOrderDAO.aggregation(params);
//          logger.info("FinanceOrderController-->aggregation result:::"+result);
//          if(result == null || result.size()<=0){
//              result = new HashMap();
//              result.put("payMoney","0");
//              result.put("slottingAllowance","0");
//              result.put("platformProfit","0");
//          }
//          return ApiResult.success(result);
//      }catch(Exception e){
//          logger.error("FinanceOrderController-->aggregation fail:",e);
//          return ApiResult.failure();
//      }
//  }
}
